package cn.mxb.logic.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

import cn.mxb.floor.pagination.Result;
import cn.mxb.floor.web.BaseAction;
import cn.mxb.logic.entity.SysCount;
import cn.mxb.logic.entity.SysTown;
import cn.mxb.logic.service.ISysCountService;
import cn.mxb.logic.service.ISysTownService;

@SuppressWarnings({ "serial", "rawtypes" })
public class SysTownAction extends BaseAction implements Preparable,ModelDriven {
	protected static final String List_JSP = "/WEB-INF/page/web/floor/townList.jsp";
	protected static final String ADD_JSP = "/WEB-INF/page/web/floor/townAdd.jsp";
	protected static final String UPDATE_JSP = "/WEB-INF/page/web/floor/townUpdate.jsp";
	
	private SysTown sysTown;
	@Autowired
	private ISysTownService SysTownService;
	@Autowired
	private ISysCountService sysCountService;
	
	private Result<SysTown> pageResult;
	
	private List<SysTown> sysTownAllList; 
	private List<SysCount> countAllList; 
	private Map<Integer,String> countNameMap = new HashMap<Integer, String>();  //县名
	
	/**
	 * @return_type: String
	 * @file_name: 跳转添加界面
	 */
	public String openAdd(){
		countAllList = sysCountService.findAllList();
		setForwardView(ADD_JSP);
		return SUCCESS;
	}
	
	/**
	 * 添加
	 * @return_type: String
	 */
	public String add(){
		SysTownService.add(sysTown);
		return openList();		
	}
	
	/**
	 * @file_name 打开列表
	 * @return 
	 */
	public String openList(){
		try{
			pageResult = SysTownService.find(sysTown, getPage(), getRow());
			for(SysTown sysTown :pageResult.getData()){
				if(null != sysTown.getSysCountId()  && !"".equals(sysTown.getSysCountId())){
					countNameMap.put(sysTown.getSysCountId(),sysCountService.findByProperty("countId",sysTown.getSysCountId()+"").getCountName());
				}
			}
			setForwardView(List_JSP);
			return SUCCESS;
		}catch(Exception e){
			System.out.println(e);
		}
		return SUCCESS;
	}
	
	/**
	 * @file_name:删除 
	 */
	public String delete(){
		SysTownService.delete(sysTown.getId());
		return openList();
	}
	
	/**
	 * @return_type: 
	 * @file_name:  打开修改页面
	 */
	public String openUpdate(){
		sysTown =  SysTownService.findByProperty("id",sysTown.getId()+"");
		countAllList = sysCountService.findAllList();
		setForwardView(UPDATE_JSP);
		return SUCCESS;
		
	}
	
	/**
	 * 修改
	 * @return_type: 
	 */
	public String update(){
		SysTownService.add(sysTown);
		sysTown = null;
		return openList();
	}
	
	
	@Override
	public Object getModel() {
		return sysTown;
	}
	@Override
	public void prepare() throws Exception {
		if(sysTown ==null){
			sysTown = new SysTown();
		}
	}

	
	public SysTown getSysTown() {
		return sysTown;
	}

	public void setSysTown(SysTown sysTown) {
		this.sysTown = sysTown;
	}

	public Result<SysTown> getPageResult() {
		return pageResult;
	}

	public void setPageResult(Result<SysTown> pageResult) {
		this.pageResult = pageResult;
	}

	
	public List<SysTown> getSysTownAllList() {
		return sysTownAllList;
	}

	public void setSysTownAllList(List<SysTown> sysTownAllList) {
		this.sysTownAllList = sysTownAllList;
	}

	public Map<Integer, String> getCountNameMap() {
		return countNameMap;
	}

	public void setCountNameMap(Map<Integer, String> countNameMap) {
		this.countNameMap = countNameMap;
	}

	public List<SysCount> getCountAllList() {
		return countAllList;
	}

	public void setCountAllList(List<SysCount> countAllList) {
		this.countAllList = countAllList;
	}

	
	
}
